﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;

namespace DungeonDelver.Engine
{
    public class IdentifiedDescriptor:Descriptor
    {
        public class IdentifiedDescriptorFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, System.Xml.Linq.XElement theXElement)
            {
                return new IdentifiedDescriptor(theParent as ConfigurationTable, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new IdentifiedDescriptor(theParent as ConfigurationTable);
            }
            private IdentifiedDescriptorFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.IdentifiedDescriptorFactory, new IdentifiedDescriptor.IdentifiedDescriptorFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.IdentifiedDescriptorFactory);
            }
        }


        public string Identifier
        {
            get
            {
                foreach (var identifier in ConfigurationTable.Identifiers)
                {
                    if (ConfigurationTable[identifier] == this)
                    {
                        return identifier;
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }
        public Game Game
        {
            get
            {
                return ConfigurationTable != null ? ConfigurationTable.Game : null;
            }
        }


        public ConfigurationTable ConfigurationTable
        {
            get
            {
                return ParentEntity as ConfigurationTable;
            }
        }

        private IdentifiedDescriptor(ConfigurationTable parent)
            : base(parent)
        {
        }
        private IdentifiedDescriptor(ConfigurationTable parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.IdentifiedDescriptor);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.IdentifiedDescriptorFactory));
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Identifier,Identifier.ToString()));
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            if (ConfigurationTable != null)
            {
                ConfigurationTable[theXElement.Attribute(PlayDeezGames.Common.Names.CommonXNames.Identifier).Value] = this;
            }
        }

        public static IdentifiedDescriptor Create(ConfigurationTable theParent)
        {
            return EntityFactories.Create(theParent, Names.EngineFactories.IdentifiedDescriptorFactory) as IdentifiedDescriptor;
        }

        public static IdentifiedDescriptor Load(ConfigurationTable theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as IdentifiedDescriptor;
        }

    }
}
